package com.goldgov.code.utils;

public class CodeUtils {

	public static final String INDENT_WIDTH = "    ";
	
	public static final String NEW_LINE = "\r\n";
	
	public static String varNew(int indentLevel,String className,String varName) {
		return varNew(indentLevel,className,varName,null);
	}
	
	public static String varNew(int indentLevel,String className,String varName,String args) {
		return indentSpace(indentLevel) + className + " " + varName + " = new " + className + "(" + nullBlank(args)  +");"+ NEW_LINE;
	}
	
	public static String varNewCustom(int indentLevel,String className,String varName,String custom) {
		return indentSpace(indentLevel) + className + " " + varName + " = " + custom  + ";"+ NEW_LINE;
	}
	
	public static String varNewCustom(int indentLevel,String className,String custom) {
		return indentSpace(indentLevel) + className + " " + StringUtils.lowerFirstChar(className) + " = " + custom  + ";"+ NEW_LINE;
	}
	
	public static String ifCondition(int indentLevel,String condition,String body) {
		return ifElseCondition(indentLevel,condition,body,null);
	}
	
	public static String ifNullCondition(int indentLevel,String varName,String body) {
		return ifElseCondition(indentLevel,varName + " != null",body,null);
	}
	
	public static String ifElseCondition(int indentLevel,String condition,String body,String elseBody) {
		StringBuilder sb = new StringBuilder();
		sb.append(indentSpace(indentLevel) + "if(" + condition  +") {" + NEW_LINE);
		sb.append(indentSpace(indentLevel +1) + body.replaceAll("\n", indentSpace(indentLevel +1)));
		if(elseBody != null) {
			sb.append(indentSpace(indentLevel) + "} else {\r\n");
			sb.append(indentSpace(indentLevel +1) + elseBody.replaceAll("\n", indentSpace(indentLevel +1)));
		}
		sb.append(NEW_LINE);
		sb.append(indentSpace(indentLevel) + "}");
		return sb.toString();
	}
	
	public static String callMethod(int indentLevel,String varName,String methodName,String... methodArgs) {
		return indentSpace(indentLevel) + varName + "." + methodName + "(" + argsSerial(methodArgs) + ");" + NEW_LINE;
	}
	
	public static String code(int indentLevel,String code) {
		return indentSpace(indentLevel) + code + "\r\n";
	}
	
	private static String nullBlank(String str) {
		return str == null ? "" : str;
	}
	
	private static String argsSerial(String... str) {
		if(str.length == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder(str[0]);
		for (int i = 1; i < str.length; i++) {
			sb.append("," + str[i]);
		}
		return sb.toString();
	}
	
	public static String indentSpace(int indentLevel) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < indentLevel; i++) {
			sb.append(INDENT_WIDTH);
		}
		return sb.toString();
	}
	
//	public static void main(String[] args) {
//		System.out.println(varNew(1,"User","user"));
//		System.out.println(varNew(1,"User","user","a"));
//		System.out.println(ifCondition(1,"a != null",varNew(0,"User","user","a")));
//	}
}
